<?php
include '../Home/DefineVar.php';
//時間の検査
function CheckTime($StartTime, $EndTime){
	if($StartTime >= $EndTime) return false;
	return true;
}
?>
<?php
//$RoomName=mysql_real_escape_string($_POST['RoomName']);
$AccountName=$_POST['AccountName'];
$RoomName=$_POST['RoomName'];
$check = true;
$StartDay1=explode("/",$_POST["startDay1"]);
$EndDay1=explode("/",$_POST["endDay1"]);
$StartDay2=explode("/",$_POST["startDay2"]);
$EndDay2=explode("/",$_POST["endDay2"]);
if($StartDay1[0]){
	$StartTime1=mktime($_POST["startTimeH1"],$_POST["startTimeM1"],0,$StartDay1[1],$StartDay1[2],$StartDay1[0]);
	if(!$EndDay1[0]) $check = false;
}
if($EndDay1[0]){
	$EndTime1=mktime($_POST["stopTimeH1"],$_POST["stopTimeM1"],0,$EndDay1[1],$EndDay1[2],$EndDay1[0]);
	if(!$StartDay1[0]) $check = false;
	else{
		if(!CheckTime($StartTime1, $EndTime1)) $check = false;
	}

}
if($StartDay2[0]){
	$StartTime2=mktime($_POST["startTimeH2"],$_POST["startTimeM2"],0,$StartDay2[1],$StartDay2[2],$StartDay2[0]);
}
else $check = false;
if($EndDay2[0]){
	$EndTime2=mktime($_POST["stopTimeH2"],$_POST["stopTimeM2"],0,$EndDay2[1],$EndDay2[2],$EndDay2[0]);
}
else $check = false;
if(!CheckTime($StartTime2, $EndTime2)) $check = false;
if(!$check){
	print("<br> <script type='text/javascript'> alert('".TIME_ERR_MESS."');  window.location ='../Admin/fine.php';</script>");
	exit();
}
include '../DataBase/ConnectDB.php';
include '../Security/CheckUser.php';
mysql_select_db(MYSQL_DATABASE,$link);
if(!$link){
	print("<br> <script type='text/javascript'> alert('".SQL_ERR_MESS."');  window.location ='../User/BookRoom.php';</script>");
}
//print "<br>Size of StartDay1: ".sizeof($StartDay1);
$queryString = "SELECT finefee FROM ".MYSQL_TABLE_ROOMINFOR." WHERE roomname = '".$RoomName."'";
$result = mysql_query($queryString,$link);
//print "<br>$queryString";
$row = mysql_fetch_array($result);
$FineFee = $row[0];
//予約したケース
if($StartDay1[0]){
	$queryString = "SELECT * FROM ".MYSQL_TABLE_ORDER." WHERE roomname = '".$RoomName."' AND orderstatus ='1'
 				AND accountname = '".$AccountName."' AND '".date("Y-m-d G:i:s",$StartTime1)."' = starttime";
	//	print "Query String : ".$queryString;
	$result = mysql_query($queryString,$link);
	$row = mysql_fetch_array($result);
	if(!$row){
		print("<br> <script type='text/javascript'> alert('".SEARCH_NOT_DATA_MESS."');  window.location ='../Admin/fine.php';</script>");
		exit();
	}
	$OrderId = $row[0];
	$Fee = $row[10];
	$OverTime = ($EndTime2 - $EndTime1)/60;
	$Fine = $FineFee * $OverTime / 30;
	$Fee = $Fee + $Fine;
	$queryString = "UPDATE ".MYSQL_TABLE_ORDER." SET overtime ='".$OverTime."', orderstatus = '2', fee = '".$Fee."' WHERE orderid = '".$OrderId."'";
	//	print "<br>Query: ".$queryString;
	$result=mysql_query($queryString,$link);
	if(!$result) print("<br> <script type='text/javascript'> alert('".FINE_ERR_MESS."');  window.location ='../Admin/fine.php';</script>");
}
//予約しないケース
else{
	$OverTime = ($EndTime2 - $StartTime2)/60;
	//	print "<br>StartTime2: ".$StartTime2;
	//	print "<br>Endtime2: ".$EndTime2;
	//	print "<br>Overtime: ".$OverTime;
	$Fee = $FineFee*$OverTime/30;
	/*$queryString = "SELECT * FROM ".MYSQL_TABLE_ORDER." WHERE roomname = '".$RoomName."'
 							AND orderstatus ='1' AND (( '".date("Y-m-d G:i:s",$StartTime2)."' <= 
 							starttime AND '".date("Y-m-d G:i:s",$EndTime2)."' > starttime ) 
 							OR ( '".date("Y-m-d G:i:s",$StartTime2)."' >= starttime 
 							AND endtime > '".date("Y-m-d G:i:s",$EndTime2)."' ))";
	$result = mysql_query($queryString,$link);
	if(!$result){
		print("<br> <script type='text/javascript'> alert('".TIME_ERR_MESS."');
 			window.location ='../User/BookRoom.php';</script>");
		exit();
	}*/
	$queryString = "INSERT INTO ".MYSQL_TABLE_ORDER."(accountname, roomname, ordertime, overtime, orderstatus, orderfee, fee)
 		 VALUES('".$AccountName."','".$RoomName."','".date("Y-m-d G:i:s",$StartTime2)."','".$OverTime."','2','0','".$Fee."')";
	//	print "<br>Query: ".$queryString;
	//	print $queryString;
	$result = mysql_query($queryString,$link);
	if(!$result) print("<br> <script type='text/javascript'> alert('".FINE_ERR_MESS."');  window.location ='../Admin/fine.php';</script>");
}
mysql_close($link);
print("<br> <script type='text/javascript'> alert('".FINE_OK_MESS."');  window.location ='../Admin/fine.php';</script>");
?>